package cn.iocoder.yudao.module.nginxui.service;

import cn.iocoder.yudao.module.nginxui.controller.admin.vo.NginxStatusVO;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

@Service
public class NginxStatusServiceImpl  implements INginxStatusService {
    @Override
    public NginxStatusVO checkLocalNginxStatus() {
        NginxStatusVO statusVO = new NginxStatusVO();
        statusVO.setExists(false);
        statusVO.setRunning(false);

        // 检查系统类型
        String osName = System.getProperty("os.name").toLowerCase();

        try {
            if (osName.contains("win")) {
                // Windows系统检测逻辑
                checkWindowsNginx(statusVO);
            } else {
                // Linux/Unix系统检测逻辑
                checkLinuxNginx(statusVO);
            }
        } catch (Exception e) {
            statusVO.setErrorMessage("检测过程发生错误: " + e.getMessage());
        }

        return statusVO;
    }

    /**
     * 检查Linux系统上的Nginx状态
     */
    private void checkLinuxNginx(NginxStatusVO statusVO) throws IOException, InterruptedException {
        // 检查是否安装了Nginx
        Process whichProcess = Runtime.getRuntime().exec("which nginx");
        int exitCode = whichProcess.waitFor();
        if (exitCode == 0) {
            statusVO.setExists(true);

            // 获取Nginx版本信息
            Process versionProcess = Runtime.getRuntime().exec("nginx -v");
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(versionProcess.getErrorStream()));
            String versionLine = errorReader.readLine();
            if (versionLine != null && versionLine.contains("nginx version:")) {
                statusVO.setVersion(versionLine.split(": ")[1]);
            }

            // 检查Nginx服务是否在运行
            Process statusProcess = Runtime.getRuntime().exec("systemctl status nginx");
            BufferedReader reader = new BufferedReader(new InputStreamReader(statusProcess.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("active (running)")) {
                    statusVO.setRunning(true);
                    break;
                }
            }
        } else {
            statusVO.setErrorMessage("未检测到Nginx安装");
        }
    }

    /**
     * 检查Windows系统上的Nginx状态
     */
    private void checkWindowsNginx(NginxStatusVO statusVO) throws IOException, InterruptedException {
        // 检查是否安装了Nginx
        Process whereProcess = Runtime.getRuntime().exec("where nginx");
        int exitCode = whereProcess.waitFor();
        if (exitCode == 0) {
            statusVO.setExists(true);

            // 获取Nginx版本信息
            Process versionProcess = Runtime.getRuntime().exec("nginx -v");
            BufferedReader errorReader = new BufferedReader(new InputStreamReader(versionProcess.getErrorStream()));
            String versionLine = errorReader.readLine();
            if (versionLine != null && versionLine.contains("nginx version:")) {
                statusVO.setVersion(versionLine.split(": ")[1]);
            }

            // 检查Nginx进程是否在运行
            Process taskProcess = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq nginx.exe\"");
            BufferedReader reader = new BufferedReader(new InputStreamReader(taskProcess.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                if (line.contains("nginx.exe")) {
                    statusVO.setRunning(true);
                    break;
                }
            }
        } else {
            statusVO.setErrorMessage("未检测到Nginx安装");
        }
    }
}
